<template>
	<view class="page-body" :class="[app.setCStyle()]">
		<view class="order-submission">
			<view class="allAddress" :style="hasKd && hasZt ? '':'padding-top:30rpx'">
				<view class="nav acea-row" v-if="hasKd && hasZt">
					<view class="item font-num" :class="shippingType == 0 ? 'on' : 'on2'" @tap="addressType(0)"
						v-if='is_shipping'></view>
					<view class="item font-num" :class="shippingType == 1 ? 'on' : 'on2'" @tap="addressType(1)"
						v-if='is_shipping'></view>
				</view>
				<view class="add-title acea-row row-between-wrapper"  v-if="!hasKd || !hasZt">
					<view class="acea-row row-middle">
						<view class="icon" :class="shippingType==1?'orange':'red'">
							{{shippingType==0?'商城配送':'预约门店'}}
						</view>
						<view class="text add-text line1" v-if="shippingType==0">由平台为您提供配送服务</view>
						<view class="text add-text line1" v-if="shippingType==1">选择就餐门店</view>
					</view>
					<view class="text" @click="goChange">{{shippingType == 0 ? '切换地址' : '切换门店'}} <text
							class='iconfont icon-jiantou'></text></view>
				</view>
				<!-- 地址 -->
				<view v-if="shippingType == 0">
					<view v-if="addressData.address_id>0">
						<navigator url="/pages/member/address/list?source=1&from=shop" class="address-section">
							<view class="address-content">
								<u-icon name="map-fill" class="ml20 mr20 base-color" size="42" ></u-icon>
								<view class="cen">
									<view class="top">
										<text class="name">{{addressData.consignee}}</text>
										<text class="mobile fs30 font-w600">{{addressData.mobile}}</text>
									</view>
									<text class="addresss fs28">{{addressData.merger_name}} {{addressData.address}}</text>
								</view>
								<u-icon name="arrow-right" class="arrow-right" color="#333333" size="20"></u-icon>
							</view>
						</navigator>
					</view>
					<view  v-else>
						<navigator url="/pages/member/address/manage?source=1&from=shop" class="address-section">
							<view class="empty-address">
								<view class="smll">
									<u-icon name="map-fill" class="ml20 mr20 base-color" size="42" ></u-icon>
									<text>选择收货地址</text>
								</view>
								<u-icon name="arrow-right" class="arrow-right" color="#333333" size="20"></u-icon>
							</view>
						</navigator>
					</view>
				</view>
				<view v-if="shippingType == 1">
					<view v-if="store.id > 0">
						<navigator :url="'/pages/user/store/index?ids='+store_ids" class="address-section">
							<view class="address-content">
								<u-icon name="map-fill" class="ml20 mr20 base-color" size="42" ></u-icon>
								<view class="cen">
									<view class="top">
										<text class="name">{{store.store_name}}</text>
										<text class="mobile fs30 font-w600">{{store.store_tel}}</text>
									</view>
									<text class="addresss fs28">{{store.store_address}}</text>
								</view>
								<u-icon name="arrow-right" class="arrow-right" color="#333333" size="20"></u-icon>
							</view>
						</navigator>
					</view>
					<view  v-else>
						<navigator :url="'/pages/user/store/index?ids='+store_ids" class="address-section">
							<view class="empty-address">
								<view class="smll">
									<u-icon name="map-fill" class="ml20 mr20 base-color" size="42" ></u-icon>
									<text>选择门店</text>
								</view>
								<u-icon name="arrow-right" class="arrow-right" color="#333333" size="20"></u-icon>
							</view>
						</navigator>
					</view>
				</view>
			</view>
		</view>
		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item" v-for="(item, index) in cartList" :key="item.rec_id">
				<image :src="baseUrl+item.pic"></image>
				<view class="ml10 flex">
					<view class="flex_bd">
						<view class="goods_name">{{item.goods_name}}</view>
						<view class="color-cc fs26 mt20">{{item.sku_name}}</view>
					</view>
					<view class="text-right price-box">
						<view class="price ff base-color">{{item.sale_price}}</view>
						<view v-if="item.use_integral > 0" class="fs26 mt10 base-color">+{{item.use_integral}}复购积分</view>
						<view class="fs30 mt10 color-99">x{{item.goods_number}}</view>
					</view>
					
				</view>
			</view>
		</view>
		<view class='wrapper' v-if="shippingType == 1">
			<view class="item acea-row row-between-wrapper" @click="showCuisine = true">
				<view>选择菜系</view>
				<view class="discount" style="display: flex;">
					<input style="text-align: right;" v-model="cuisineName" type="text" placeholder="请选择菜系"
						placeholder-class="placeholder" disabled></input>
					<u-icon name="arrow-right" color="#999" size="32" style="position: relative;top: 2rpx;"></u-icon>
					<u-select v-model="showCuisine" :list="cuisineList" value-name="id" label-name="name" @confirm="confirmCuisine"></u-select>
				</view>
			</view>
			<view class="item acea-row row-between-wrapper" @click="showTime = true">
				<view>预约时间</view>
				<view class="discount" style="display: flex;">
					<input style="text-align: right;" v-model="reservation_time" type="text" placeholder="请选择预约时间"
						placeholder-class="placeholder" disabled></input>
					<u-icon name="arrow-right" color="#999" size="32" style="position: relative;top: 2rpx;"></u-icon>
					<u-picker v-model="showTime" mode="time" :params="params" start-year="2025" @confirm="confirmTime"></u-picker>
				</view>
			</view>
			<view class="item acea-row row-between-wrapper">
				<view>联系人姓名</view>
				<view class="discount">
					<input style="text-align: right;" v-model="contacts" type="text" placeholder="请输入联系人姓名"
						placeholder-class="placeholder"></input>
				</view>
			</view>
			<view class="item acea-row row-between-wrapper">
				<view>联系电话</view>
				<view class="discount">
					<input style="text-align: right;" v-model="contactsTel" type="text"
						placeholder="请输入联系手机号码" placeholder-class="placeholder"></input>
				</view>
			</view>
			<view class="item acea-row row-between-wrapper">
				<view>就餐人数</view>
				<view class="discount">
					<u-number-box v-model="number" :min="1" @change="valChange"></u-number-box>
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell">
				<text class="cell-tit clamp">用餐费用</text>
				<text class="cell-tip ff base-color">{{price}}人</text>
			</view>
			<!-- 运费 -->
			<view class="yt-list-cell">
				<text class="cell-tit clamp">剩余餐券</text>
				<text class="cell-tip ff base-color">{{userInfo.cws}}张</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请输入备注内容" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content base-color">
				<text class="fs28">合计:</text>
				<text class="price ff">{{order_total}}</text>
				<text v-if="integralTotal > 0" class="fs30 ml10">+{{integralTotal}}复购积分</text>
			</view>
			<text class="submit primarybtn" :loading="isLoading" @click="submit">确认提交</text>
		</view>
		<bonusSelect  @toggleBonusSelect="toggleBonusSelect"  :bonusSelectClass="bonusSelectClass" :bonusList="bonusList"></bonusSelect>
	</view>
</template>

<script>
	import bonusSelect from '@/pages/shop/bonus/select';
	export default {
		components: {
			bonusSelect
		},
		data() {
			return {
				shipping_fee_ing: 1,
				setting: {},
				baseUrl: this.config.baseUrl,
				isLoading: false,
				desc: '', //备注
				address_id: 0,
				addressData: {},
				recids: '',
				cartList: [],
				goods_total: 0,
				shipping_fee: 0,
				order_total: 0,
				integralTotal: 0,
				bonusSelectClass: 'none',
				used_bonus_id:0,
				showBonus:false,//setting.sys_model_shop_bonus
				bonusList:{},
				bonusMoney:0,
				bonusTip:'选择优惠券',
				hasKd:0,
				hasZt:1,
				shippingType:1,
				is_shipping: true,
				store_id:0,
				store:{},
				contacts:'',
				contactsTel:'',
				store_ids:'',
				userInfo:{},
				number:1,
				cuisineList:[],
				cuisine:0,
				cuisineName:'',
				showCuisine:false,
				price:0,
				reservation_time:'',
				showTime:false,
				params:{
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false,
					// 选择时间的时间戳
					timestamp: true,
				}
			}
		},
		onLoad(options) {
			this.app.isLogin(this); //强制登陆
			this.setting = uni.getStorageSync("setting");
			this.recids = options.recids;
			this.getAddress();
			this.loadData();
		},
		onShow: function() {
			let _this = this
			uni.$on("handClick", res => {
				if (res) {
					_this.store = res.address
					_this.store_id = res.address.id
				}
				// 清除监听
				uni.$off('handClick');
			})
		},
		methods: {
			//请求数据
			async loadData() {
				this.hasZt = 1
				this.getCuisineList();
				this.getCenterInfo();
				this.getStoreList();
			},
			getCenterInfo() {
				//获取会员中心信息
				this.$u.post('member/api.center/getCenterInfo').then(res => {
					this.userInfo = res.data.userInfo;
					this.valChange({value:1})
				});
			},
			getCuisineList() {
				//获取会员中心信息
				this.$u.post('shop/api.order/cuisineList').then(res => {
					this.cuisineList = res.data
					this.price = this.cuisineList[0].price
					this.cuisineName = this.cuisineList[0].name
					this.cuisine = this.cuisineList[0].id
				});
			},
			valChange(e){
				if(Number(this.userInfo.cws) > Number(this.number)){
					this.order_total = 0
				}else{
					this.order_total = (this.number-this.userInfo.cws) * this.price
				}
				
			},
			//请求数据
			async getAddress() {
				let address_id = uni.getStorageSync("address_id");
				this.$u.post('member/api.address/getAddress', {
					address_id: address_id
				}).then(res => {
					this.addressData = res.data;
					if (this.addressData.address_id > 0) {
						this.address_id = this.addressData.address_id;
						this.contacts = this.addressData.consignee
						this.contactsTel = this.addressData.mobile
					} else {
						this.address_id = 0;
					}

				})
			},

			//提交订单
			submit() {
				if (this.isLoading == true) {
					return false;
				}
				var data = {};
				if(!this.store_id){
					uni.showToast({
						title: '请选择提货门店',
						icon: 'none'
					});
					return
				}
				if(!this.reservation_time){
					uni.showToast({
						title: '请选择预约时间',
						icon: 'none'
					});
					return
				}
				if(!this.contacts){
					uni.showToast({
						title: '请输入联系人姓名',
						icon: 'none'
					});
					return
				}
				if(!this.contactsTel){
					uni.showToast({
						title: '请输入联系电话',
						icon: 'none'
					});
					return
				}
				data.store_id = this.store_id;
				data.consignee = this.contacts
				data.mobile = this.contactsTel
				data.cuisine = this.cuisine
				data.reservation_time = this.reservation_time
				data.number = this.number
				data.desc = this.desc
				this.isLoading = true;
				let _this = this;
				this.$u.post('shop/api.flow/reservation', data).then(res => {
					let is_pay = res.data.is_pay;
					let order_id = res.data.id;
					if (is_pay == 1) {
						uni.redirectTo({
							url: `/pages/shop/order/pay?order_id=${order_id}&from=reservation`
						});
					}else{
						uni.redirectTo({
							url: `/pages/public/pay/paySuccess?from=reservation&payType=order&order_id=${order_id}`
						});
					}
				}).catch(res => {
					this.isLoading = false;
				})

			},
			getWxLocation(){
				let _this = this;
				var url = location.href.split('#')[0];
				this.$u.post('/publics/api.index/getWxShare', {
					url: url,
				}).then(res => {
					uni.hideLoading();
					var shareConfig = res.data;
					_this.token = res.data.token
					_this.$wxjs.config({
						debug: false,
						appId: res.data.wxShare.appId,
						timestamp: res.data.wxShare.timestamp,
						nonceStr: res.data.wxShare.nonceStr,
						signature: res.data.wxShare.signature,
						jsApiList: ['getLocation','openLocation']})
						_this.$wxjs.ready(() => {
							_this.$wxjs.getLocation({
								type: 'gcj02',
								success: (res) => {
									_this.tips(res.latitude)
									uni.setStorageSync('user_latitude', res.latitude);
									uni.setStorageSync('user_longitude', res.longitude);
								},
								complete: () => {
									_this.getStoreList()
								},
							});
						})
				}).catch(err => {
					console.log(err);
				})
			},
			// 获取门店列表数据
			getStoreList: function() {
				let longitude = uni.getStorageSync("user_longitude") || ''; //经度
				let latitude = uni.getStorageSync("user_latitude") || ''; //纬度
				let data = {
					latitude: latitude, //纬度
					longitude: longitude, //经度
					page: 1,
					ids:this.store_ids
				};
				this.$u.get('shop/api.store/lst', data).then(res => {
					if(res.data.data.length > 0){
						this.store = res.data.data[0]
						this.store_id = res.data.data[0].id
					}
				})
				.catch(err => {
					this.$util.Tips({
						title: err
					})
				});
			},
			goChange(){
				if(this.shippingType == 0){
					uni.navigateTo({
						url:'/pages/member/address/list?source=1&from=shop'
					})
				}else{
					uni.navigateTo({
						url:'/pages/user/store/index?ids='+this.store_ids
					})
				}
			},
			stopPrevent() {},
			confirmCuisine(e){
				this.cuisineName = e[0].label
				this.cuisine = e[0].value
				this.cuisineList.forEach(item => {
					if(item.id == this.cuisine){
						this.price = item.price
					}
				})
				this.valChange({value:this.number})
			},
			confirmTime(e){
				this.reservation_time = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100rpx;
	}


	.address-section {
		padding: 30rpx 0;
		background: #fff;
		position: relative;
		margin: 0rpx 20rpx 20rpx 20rpx;
		border-radius: 0px 0px 10px 10px;

		.empty-address {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			font-weight: 600;
			padding: 20rpx 0;
		}

		.address-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90rpx;
			color: #888;
			font-size: 44rpx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28rpx;
			color: $font-color-dark;
		}

		.name {
			font-size: 30rpx;
			margin-right: 24rpx;
		}

		.addresss {
			margin-top: 16rpx;
			margin-right: 20rpx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32rpx;
			color: $font-color-light;
			margin-right: 30rpx;
		}

		.arrow-right {
			position: absolute;
			right: 10rpx;
			top: 40%;
		}
	}

	.goods-section {
		margin: 20rpx;
		border-radius: 10px;
		background: #fff;

		.g-header {
			display: flex;
			align-items: center;
			height: 84rpx;
			padding: 0 30rpx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50rpx;
			height: 50rpx;
			border-radius: 100px;
		}

		.name {
			font-size: 30rpx;
			color: $font-color-base;
			margin-left: 24rpx;
		}

		.g-item {
			display: flex;
			padding: 20rpx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
			}

			.goods_name {
				display: block;
				font-size: 28rpx;
				color: $font-color-dark;
				line-height: 40rpx;
				height: 80rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}


			.price-box {
				font-size: 40rpx;
				color: $font-color-dark;
				line-height: 1;

				.price {
					font-size: 40rpx;
					line-height: 1;

					&:before {
						content: '￥';
						font-size: 24rpx;
					}
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin: 20rpx;
		border-radius: 10px;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 30rpx 20rpx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			color: $font-color-light;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			flex: 1;
			font-size: 28rpx;
			color: $font-color-dark;
			margin-right: 10rpx;
		}

		.cell-tip {

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $font-color-base;
			}

			&.red {
				color: $font-color-base;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 98rpx;
		justify-content: flex-end;
		font-size: 30rpx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		padding: 0 20rpx;

		.price-content {
			padding-right: 30rpx;
		}

		.price-tip {
			color: $font-color-red;
			margin-left: 8rpx;
		}

		.price {
			font-size: 40rpx;
			&:before {
				content: '￥';
				font-size: 24rpx;
			}
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 200rpx;
			height: 70rpx;
			color: #fff;
			border-radius: 40rpx;
			font-size: 28rpx;
		}
	}
	.order-submission .allAddress {
		width: 100%;
		background: linear-gradient(to bottom, #ec4179 0%, #f5f5f5 100%);
		padding-top: 100rpx;
		margin-bottom: 12rpx;

		.add-title {
			height: 72rpx;
			margin: 0 26rpx;
			border-bottom: 1px solid #eee;
			background-color: #fff;
			width: 710rpx;
			margin: 0 auto;
			padding: 0 26rpx;
			border-radius: 12rpx 12rpx 0 0;
			display: flex;
			justify-content: space-between;
			.icon {
				height: 32rpx;
				background: #1890FF;
				border-radius: 4rpx;
				font-size: 20rpx;
				font-weight: 400;
				color: #FFFFFF;
				text-align: center;
				line-height: 32rpx;
				padding: 0 6rpx;

				&.orange {
					background: #FE960F;
				}

				&.red {
					background: #E93323;
				}
			}

			.add-text {
				margin-left: 14rpx;
				width: 360rpx;
			}

			.text {
				font-size: 24rpx;
				font-weight: 400;
				color: #999999;

				.icon-jiantou {
					display: inline-block;
					font-size: 20rpx;
				}
			}
		}
	}
	.order-submission .allAddress .nav {
		width: 710rpx;
		margin: 0 auto;
	}
	
	.order-submission .allAddress .nav .item {
		width: 355rpx;
		display: inline-block;
	}
	
	.order-submission .allAddress .nav .item.on {
		position: relative;
		width: 250rpx;
	}
	
	.order-submission .allAddress .nav .item.on::before {
		position: absolute;
		bottom: 0;
		content: "快递配送";
		font-size: 28rpx;
		display: block;
		height: 0;
		width: 336rpx;
		border-width: 0 20rpx 80rpx 0;
		border-style: none solid solid;
		border-color: transparent transparent #fff;
		z-index: 2;
		border-radius: 7rpx 30rpx 0 0;
		text-align: center;
		line-height: 80rpx;
		color: #f31c62;
	}
	
	.order-submission .allAddress .nav .item:nth-of-type(2).on::before {
		content: "到店自提";
		border-width: 0 0 80rpx 20rpx;
		border-radius: 30rpx 7rpx 0 0;
	}
	
	.order-submission .allAddress .nav .item.on2 {
		position: relative;
	}
	
	.order-submission .allAddress .nav .item.on2::before {
		position: absolute;
		bottom: 0;
		content: "到店自提";
		font-size: 28rpx;
		display: block;
		height: 0;
		width: 400rpx;
		border-width: 0 0 60rpx 60rpx;
		border-style: none solid solid;
		border-color: transparent transparent rgba(255, 255, 255, 0.6);
		border-radius: 40rpx 6rpx 0 0;
		text-align: center;
		line-height: 60rpx;
		background-color: #f31c62;
		color: #f31c62;
	}
	
	.order-submission .allAddress .nav .item:nth-of-type(1).on2::before {
		content: "快递配送";
		border-width: 0 60rpx 60rpx 0;
		border-radius: 6rpx 40rpx 0 0;
	}
	
	.order-submission .allAddress .address {
		width: 710rpx;
		height: 150rpx;
		margin: 0 auto;
	}
	
	.order-submission .allAddress .line {
		width: 710rpx;
		margin: 0 auto;
	}
	
	.wrapper {
		background-color: #fff;
		margin: 13rpx 20rpx;
		border-radius: 10rpx;
	}
	
	.wrapper .item {
		padding: 27rpx 30rpx;
		font-size: 30rpx;
		color: #282828;
		border-bottom: 1px solid #f0f0f0;
	}
	
</style>
